/*
 * LoginDialogView.java
 *
 * Created on August 16, 2008, 10:58 PM
 */
package com.indragunawan.restobiz.app;

import java.awt.event.KeyEvent;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import org.jdesktop.application.Action;

/**
 *
 * @author  igoens
 */
public class LoginDialogView extends javax.swing.JDialog {
    
    private static final long serialVersionUID = 1186601939378028050L;

    private GeneralConfig cfg = new GeneralConfig();
    private DatabaseSetupView dbSetup;

    /** Creates new form LoginDialogView */
    public LoginDialogView(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();

        resetInputForm();

        if (cfg.isStandardLogin()) {
            statusLabel.setText("Default login & password = 'admin'");
        } else {
            statusLabel.setText("");
        }

        setupButton.setVisible(!cfg.isDbConnected());
        loginButton.setEnabled(cfg.isDbConnected());
    }

    private void resetInputForm() {
        userField.setText("");
        passwordField.setText("");

        userField.requestFocus();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        headerPanel = new javax.swing.JPanel();
        judulLabel = new javax.swing.JLabel();
        subJudulLabel = new javax.swing.JLabel();
        iconImageLabel = new javax.swing.JLabel();
        contentPanel = new javax.swing.JPanel();
        userLabel = new javax.swing.JLabel();
        userField = new javax.swing.JTextField();
        passwordLabel = new javax.swing.JLabel();
        passwordField = new javax.swing.JPasswordField();
        footerPanel = new javax.swing.JPanel();
        keluarButton = new javax.swing.JButton();
        loginButton = new javax.swing.JButton();
        statusLabel = new javax.swing.JLabel();
        setupButton = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(com.indragunawan.restobiz.app.MainApp.class).getContext().getResourceMap(LoginDialogView.class);
        setTitle(resourceMap.getString("restobizLogin.title")); // NOI18N
        setName("restobizLogin"); // NOI18N
        setResizable(false);
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                formWindowClosing(evt);
            }
        });

        headerPanel.setBackground(resourceMap.getColor("headerPanel.background")); // NOI18N
        headerPanel.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        headerPanel.setName("headerPanel"); // NOI18N

        judulLabel.setFont(resourceMap.getFont("judulLabel.font")); // NOI18N
        judulLabel.setText(resourceMap.getString("judulLabel.text")); // NOI18N
        judulLabel.setName("judulLabel"); // NOI18N

        subJudulLabel.setText(resourceMap.getString("subJudulLabel.text")); // NOI18N
        subJudulLabel.setName("subJudulLabel"); // NOI18N

        iconImageLabel.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        iconImageLabel.setIcon(resourceMap.getIcon("iconImageLabel.icon")); // NOI18N
        iconImageLabel.setText(resourceMap.getString("iconImageLabel.text")); // NOI18N
        iconImageLabel.setVerticalAlignment(javax.swing.SwingConstants.TOP);
        iconImageLabel.setAlignmentY(0.0F);
        iconImageLabel.setName("iconImageLabel"); // NOI18N

        javax.swing.GroupLayout headerPanelLayout = new javax.swing.GroupLayout(headerPanel);
        headerPanel.setLayout(headerPanelLayout);
        headerPanelLayout.setHorizontalGroup(
            headerPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(headerPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(headerPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(headerPanelLayout.createSequentialGroup()
                        .addGap(12, 12, 12)
                        .addComponent(subJudulLabel))
                    .addComponent(judulLabel))
                .addGap(117, 117, 117)
                .addComponent(iconImageLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 42, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
        headerPanelLayout.setVerticalGroup(
            headerPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, headerPanelLayout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(headerPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(iconImageLabel)
                    .addGroup(headerPanelLayout.createSequentialGroup()
                        .addComponent(judulLabel)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(subJudulLabel)))
                .addContainerGap())
        );

        contentPanel.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        contentPanel.setName("contentPanel"); // NOI18N

        userLabel.setDisplayedMnemonic('U');
        userLabel.setLabelFor(userField);
        userLabel.setText(resourceMap.getString("userLabel.text")); // NOI18N
        userLabel.setName("userLabel"); // NOI18N

        userField.setText(resourceMap.getString("userField.text")); // NOI18N
        userField.setName("userField"); // NOI18N
        userField.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                userFieldKeyPressed(evt);
            }
        });

        passwordLabel.setDisplayedMnemonic('P');
        passwordLabel.setLabelFor(passwordField);
        passwordLabel.setText(resourceMap.getString("passwordLabel.text")); // NOI18N
        passwordLabel.setName("passwordLabel"); // NOI18N

        passwordField.setText(resourceMap.getString("passwordField.text")); // NOI18N
        passwordField.setName("passwordField"); // NOI18N
        passwordField.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                passwordFieldKeyPressed(evt);
            }
        });

        javax.swing.GroupLayout contentPanelLayout = new javax.swing.GroupLayout(contentPanel);
        contentPanel.setLayout(contentPanelLayout);
        contentPanelLayout.setHorizontalGroup(
            contentPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(contentPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(contentPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(passwordLabel)
                    .addComponent(userLabel))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(contentPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(passwordField, javax.swing.GroupLayout.DEFAULT_SIZE, 296, Short.MAX_VALUE)
                    .addComponent(userField, javax.swing.GroupLayout.DEFAULT_SIZE, 296, Short.MAX_VALUE))
                .addContainerGap())
        );

        contentPanelLayout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {passwordLabel, userLabel});

        contentPanelLayout.setVerticalGroup(
            contentPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(contentPanelLayout.createSequentialGroup()
                .addGap(24, 24, 24)
                .addGroup(contentPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(userLabel)
                    .addComponent(userField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(contentPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(passwordLabel)
                    .addComponent(passwordField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(26, Short.MAX_VALUE))
        );

        footerPanel.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        footerPanel.setName("footerPanel"); // NOI18N

        javax.swing.ActionMap actionMap = org.jdesktop.application.Application.getInstance(com.indragunawan.restobiz.app.MainApp.class).getContext().getActionMap(LoginDialogView.class, this);
        keluarButton.setAction(actionMap.get("quit")); // NOI18N
        keluarButton.setMnemonic('x');
        keluarButton.setName("keluarButton"); // NOI18N
        keluarButton.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                keluarButtonKeyPressed(evt);
            }
        });

        loginButton.setAction(actionMap.get("validasiLogin")); // NOI18N
        loginButton.setMnemonic('L');
        loginButton.setText(resourceMap.getString("loginButton.text")); // NOI18N
        loginButton.setName("loginButton"); // NOI18N
        loginButton.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                loginButtonKeyPressed(evt);
            }
        });

        statusLabel.setText(resourceMap.getString("statusLabel.text")); // NOI18N
        statusLabel.setName("statusLabel"); // NOI18N

        setupButton.setAction(actionMap.get("showDatabaseSetup")); // NOI18N
        setupButton.setMnemonic('S');
        setupButton.setText(resourceMap.getString("setupButton.text")); // NOI18N
        setupButton.setName("setupButton"); // NOI18N
        setupButton.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                setupButtonKeyPressed(evt);
            }
        });

        javax.swing.GroupLayout footerPanelLayout = new javax.swing.GroupLayout(footerPanel);
        footerPanel.setLayout(footerPanelLayout);
        footerPanelLayout.setHorizontalGroup(
            footerPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, footerPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(statusLabel)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 102, Short.MAX_VALUE)
                .addComponent(setupButton)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(loginButton)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(keluarButton)
                .addContainerGap())
        );

        footerPanelLayout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {keluarButton, loginButton});

        footerPanelLayout.setVerticalGroup(
            footerPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, footerPanelLayout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(footerPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(loginButton)
                    .addComponent(keluarButton)
                    .addComponent(statusLabel)
                    .addComponent(setupButton))
                .addContainerGap())
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(headerPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addComponent(footerPanel, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addComponent(contentPanel, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(headerPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(contentPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(footerPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
        );

        getAccessibleContext().setAccessibleName(resourceMap.getString("Restobiz Login.AccessibleContext.accessibleName")); // NOI18N

        pack();
    }// </editor-fold>//GEN-END:initComponents

private void loginButtonKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_loginButtonKeyPressed
    if (evt.getKeyCode() == KeyEvent.VK_ENTER) {
        validasiLogin();
    }
}//GEN-LAST:event_loginButtonKeyPressed

private void userFieldKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_userFieldKeyPressed
    if (evt.getKeyCode() == KeyEvent.VK_ENTER) {
        evt.getComponent().transferFocus();
    }
}//GEN-LAST:event_userFieldKeyPressed

private void passwordFieldKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_passwordFieldKeyPressed
    if (evt.getKeyCode() == KeyEvent.VK_ENTER) {
        evt.getComponent().transferFocus();
    }
}//GEN-LAST:event_passwordFieldKeyPressed

private void setupButtonKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_setupButtonKeyPressed
    if (evt.getKeyCode() == KeyEvent.VK_ENTER) {
        showDatabaseSetup();
    }
}//GEN-LAST:event_setupButtonKeyPressed

private void keluarButtonKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_keluarButtonKeyPressed
    if (evt.getKeyCode() == KeyEvent.VK_ENTER) {
        System.exit(0);
    }
}//GEN-LAST:event_keluarButtonKeyPressed

private void formWindowClosing(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosing
    if (cfg.getLoggedUser().isEmpty()) {
        System.exit(0);
    }
}//GEN-LAST:event_formWindowClosing

    @Action
    public void validasiLogin() {
        if (cfg.confirmConnected()) {
            String User;
            String Password;

            User = userField.getText();
            Password = String.valueOf(passwordField.getPassword());

            if (cfg.isValidUser(User, Password)) {
                cfg.setLoggedUser(User);
                statusLabel.setText("Login sukses ...");
                this.dispose();
            } else {
                statusLabel.setText("Login gagal ...");
                resetInputForm();
            }
        } else {
            JOptionPane.showMessageDialog(this, "Lakukan konfigurasi database terlebih dahulu");
        }
    }

    @Action
    public void showDatabaseSetup() {
        this.setVisible(false);

        Boolean confirmed = true;

        if (cfg.isDbConnected()) {
            if (JOptionPane.showConfirmDialog(null, "Koneksi database telah terhubung dengan baik.\nAnda yakin akan masuk ke konfigurasi database?", "Konfirmasi", JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION) {
                confirmed = true;
            } else {
                confirmed = false;
            }
        }

        if (confirmed) {
            if (dbSetup == null) {
                if ((dbSetup != null) && (!dbSetup.isVisible())) {
                    dbSetup = null;
                }
                JFrame mainFrame = MainApp.getApplication().getMainFrame();
                dbSetup = new DatabaseSetupView(mainFrame, true);
                dbSetup.setLocationRelativeTo(mainFrame);
            }
            MainApp.getApplication().show(dbSetup);
        }
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JPanel contentPanel;
    private javax.swing.JPanel footerPanel;
    private javax.swing.JPanel headerPanel;
    private javax.swing.JLabel iconImageLabel;
    private javax.swing.JLabel judulLabel;
    private javax.swing.JButton keluarButton;
    private javax.swing.JButton loginButton;
    private javax.swing.JPasswordField passwordField;
    private javax.swing.JLabel passwordLabel;
    private javax.swing.JButton setupButton;
    private javax.swing.JLabel statusLabel;
    private javax.swing.JLabel subJudulLabel;
    private javax.swing.JTextField userField;
    private javax.swing.JLabel userLabel;
    // End of variables declaration//GEN-END:variables
}
